package com.tsd.jxc.stock.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.stock.dao.JxcStockInbillItemMapper;
import com.tsd.jxc.stock.dao.JxcStockInbillMapper;
import com.tsd.jxc.stock.entity.JxcStockInbillExt;
import com.tsd.jxc.stock.entity.JxcStockInbillItem;
import com.tsd.jxc.stock.entity.JxcStockInbillItemExt;
import com.tsd.jxc.stock.service.JxcStockInbillItemService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcStockInbillItem 服务实现层
 *
 * @author Hillpool
 * @date 2022/07/29 15:06:33
 */
@Service
public class JxcStockInbillItemServiceImpl extends BaseServiceImpl implements JxcStockInbillItemService {

    @Resource
    private JxcStockInbillItemMapper jxcStockInbillItemMapper;
    @Resource
    private JxcStockInbillMapper jxcStockInbillMapper;

    @Override
    public JxcStockInbillItem getJxcStockInbillItemById(Long id) throws Exception {
        return jxcStockInbillItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcStockInbillItemExt queryBySid(String sid) throws Exception {
        return jxcStockInbillItemMapper.selectBySid(sid);
    }

    @Override
    public List<JxcStockInbillItemExt> queryValidByBillList(List<String> bill_list) throws Exception {
        List<JxcStockInbillItemExt> itemExtList = ThreadPoolTaskUtil.queryInList4Split(bill_list, list -> {
            JxcStockInbillItemExt params = new JxcStockInbillItemExt();
            params.setDr(0);
            params.setBill_list(list);
            return jxcStockInbillItemMapper.selectByEntity(params);
        });
        return itemExtList;
    }

    @Override
    public void deleteJxcStockInbillItems(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcStockInbillItem old = jxcStockInbillItemMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcStockInbillItemMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcStockInbillItem(JxcStockInbillItemExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcStockInbillItemMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcStockInbillItemMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveJxcStockInbillItem(JxcStockInbillExt record, SysUser opUser) throws Exception {
        List<JxcStockInbillItemExt> itemList = record.getItem_list();
        super.checkEmptyList(itemList, "单据明细不能为空");

        List<Long> existIdList = ListUtil.filterMap(itemList, t -> !HlpUtils.isEmpty(t.getId()), JxcStockInbillItemExt::getId);

        JxcStockInbillItemExt params = new JxcStockInbillItemExt();
        params.setDr(0);
        params.setInbill_sid(record.getSid());
        List<JxcStockInbillItemExt> oldList = jxcStockInbillItemMapper.selectByEntity(params);

        //删除不存在订单明细
        List<JxcStockInbillItemExt> delList = ListUtil.filter(oldList, t -> !existIdList.contains(t.getId()));
        if (!HlpUtils.isEmptyList(delList)) {
            List<Long> delIds = ListUtil.map(delList, JxcStockInbillItemExt::getId);
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIds);
            jxcStockInbillItemMapper.deleteLogicByIdList(logicVo);

            List<String> delSids = ListUtil.map(delList, JxcStockInbillItemExt::getSid);
            logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setSidList(delSids);
        }

        for (JxcStockInbillItemExt itemExt : itemList) {
            super.checkEmpty(itemExt.getIn_qty(), "第" + itemExt.getSn() + "项明细数量不能为空");
            super.checkEmpty(itemExt.getPrice(), "第" + itemExt.getSn() + "项明细价格不能为空");
            itemExt.setInbill_sid(record.getSid());
            if (HlpUtils.isEmpty(itemExt.getAmount())) {
                double amount = DoubleUtil.mul(itemExt.getIn_qty(), itemExt.getPrice());
                itemExt.setAmount(BigDecimal.valueOf(amount));
            }
            this.saveJxcStockInbillItem(itemExt, opUser);
        }
        jxcStockInbillMapper.updateInQty(record.getSid());
        jxcStockInbillItemMapper.updateQty4baseByBillSid(record.getSid());
    }

    @Override
    public List<JxcStockInbillItemExt> findJxcStockInbillItems(Map<String, Object> params, PageBean page) throws Exception {
        return jxcStockInbillItemMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcStockInbillItemExt> findJxcStockInbillItems(Map<String, Object> params) throws Exception {
        return jxcStockInbillItemMapper.selectByParams(params);
    }
}
